package com.ace.lab.starter.config;

import com.ace.lab.starter.launcher.RpcServerLauncher;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.curator.framework.CuratorFramework;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
@EnableConfigurationProperties({RpcServerProperties.class, ZookeeperProperties.class, DruidDataSourceProperties.class})
public class ServiceProducerConfiguration {

    @Autowired
    private RpcServerProperties producerProperties;
    @Autowired
    private CuratorFramework curatorFramework;
    @Autowired
    private DruidDataSourceProperties dataSourceProperties;

    @Bean
    public RpcServerLauncher thriftServerLauncher() {
        return new RpcServerLauncher(producerProperties, curatorFramework);
    }

    @Bean
    @ConditionalOnMissingBean
    public DataSource druidDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(dataSourceProperties.getDriverClassName());
        dataSource.setUrl(dataSourceProperties.getUrl());
        dataSource.setUsername(dataSourceProperties.getUsername());
        dataSource.setPassword(dataSourceProperties.getPassword());
        return dataSource;
    }

}
